package com.infinitegroup.findmystuff.presenters;

import java.util.List;
import java.util.Map;

import android.content.Context;

import com.infinitegroup.findmystuff.models.Account;
import com.infinitegroup.findmystuff.models.AccountLink;
import com.infinitegroup.findmystuff.models.Admin;
import com.infinitegroup.findmystuff.views.AdminHomeView;

public class AdminHomePresenter {
	
	private AdminHomeView view;
	private Admin admin;
	private List<Map<String,String>> lockedAccounts;
	
	/**
	 * Constructor for the AdminHomePresenter.
	 * @param view The view associated with the activity the presenter controls.
	 * @param username Username of the account logged into.
	 */
	public AdminHomePresenter(AdminHomeView view, String username){
		this.view = view;
		admin = new AccountLink(username, (Context) view);	
	}
	
	/**
	 * Gets locked accounts associated with the username and returns them as a list of maps.
	 * @return List of items (maps).
	 */
	public List<Map<String,String>> getLockedAccountsToDisplay() {
		try{
			lockedAccounts = admin.getLockedAccounts();
			if ( lockedAccounts != null && (!lockedAccounts.isEmpty())) {
				return null;
			} else {
				return lockedAccounts;
			}
		} catch (Exception e){
			view.displayMessage("Could not fetch locked accounts from database");
			return null;
		}
	}
	
	/**
	 * Gets the first name of the admin account currently logged into.
	 * @return
	 */
	public String getFirstName() {
		return admin.getFirstName();
	}

	/**
	 * Handles clicking of the unlock button by unlocking the account associated with the inputed
	 * username if it exists and is actually locked. If it does unlock an account, it also updates
	 * the list of locked accounts that are displayed.
	 * @param username Username of the account to unlock.
	 */
	public void handleUnlockClick(String username) {
		try{
			Account account = new AccountLink(username, (Context) this.view);
			String userid = account.getId();
			if ((!userid.equals("")) && (!account.isActive())){
				admin.unlockAccount(userid); // delete account from database.
				view.displayMessage("Account successfully unlocked");
				for (Map<String, String> m : this.lockedAccounts){
					if (m.get("username").equals(username)){
						lockedAccounts.remove(m); // remove the map/user you just unlocked from the local list also
					}
				}
				view.refreshString(lockedAccounts);
			} else {
				view.displayMessage("No such user exists or account is already active");
			}

		} catch (Exception e){
			view.displayMessage("Errors encountered while connecting to server.");
		}
	}

	/**
	 * Handles click of delete button by removing the account associated with the inputed username
	 * from the database if the account exists and it is not the superadmin.
	 * 
	 * @param username Username of account to deete.
	 */
	public void handleDeleteClick(String username) {
		try{
			if(!username.equals("superadmin")){
				Account account = new AccountLink(username, (Context) this.view);
				if (account.getUsername().equals("")){ // i.e. if no such user exists
					view.displayMessage("No such user exists");
				} 
				else {
					String userid = account.getId();
					admin.deleteAccount(userid);
					view.displayMessage("Account Deleted");
					view.refreshString(lockedAccounts);
					if (lockedAccounts!=null){
						for (Map<String, String> m : this.lockedAccounts) {
							if (m.get(Constants.ACCOUNT_USERNAME).equals(username)) {
								lockedAccounts.remove(m);
							}
						}
					}
				}
			} 
			else {
				view.displayMessage("You can't kill Chuck Norris.");
			}

		} catch (Exception e){
			e.printStackTrace();
			view.displayMessage("Errors encountered while connecting to server.");
		}
	}

	/**
	 * Handles click of registration button by telling the view to advance to the admin
	 * registration screen.
	 */
	public void handleRegisterClick() {
		view.advanceToAdminRegistration(admin.getUsername());		
	}
}
